package com.yuanli.cosmeticmirror.base;

import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.ColorInt;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.ColorUtils;

import com.comenjoysoft.baselibrary.base.AppManager;
import com.gyf.immersionbar.ImmersionBar;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.TipDialog;
import com.kongzue.dialog.v3.WaitDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity {

    protected final String TAG = this.getClass().getSimpleName();
    private TipDialog dialog;
    /**
     * activity堆栈管理
     */
    protected AppManager appManager = AppManager.getAppManager();



    /**
     * View点击
     **/
    protected abstract int getContentView();

    /**
     * 初始化
     */
    protected abstract void init(Bundle bundle);

    /**
     * 初始化控件
     */
    protected abstract void initView();

    protected abstract void initData();

    protected abstract void destroy();

    Unbinder unbinder;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        //解决底部布局会被EditText挤出
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN |
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        ImmersionBar.with(this).statusBarDarkFont(true, 0.2f).init();
        setContentView(getContentView());
        unbinder = ButterKnife.bind(this);
        appManager.addActivity(this);
        init(savedInstanceState);
        initView();
        initData();
    }


    /**
     * 判断颜色是不是亮色
     *
     * @param color
     * @return
     * @from https://stackoverflow.com/questions/24260853/check-if-color-is-dark-or-light-in-android
     */
    private boolean isLightColor(@ColorInt int color) {
        return ColorUtils.calculateLuminance(color) >= 0.5;
    }

    @Override
    protected void onDestroy() {
        destroy();
        unbinder.unbind();
        appManager.removeActivity(this);
        super.onDestroy();
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public void dissmis() {
        if (dialog != null && dialog.isShow) {
            dialog.doDismiss();
        }
    }


    public void showLoadDialog(String msg) {
        dialog = null;
        dialog = WaitDialog.show(this, msg);
    }

    /**
     * 正在加载。。。
     */
    public void showLoadDialog() {
        dialog = null;
        dialog = WaitDialog.show(this, "正在加载...");
    }

    public void dialogDismiss() {
        if (dialog != null && dialog.isShow) {
            dialog.doDismiss();
        }
    }



}
